package com.hongxun.design.ui.moveseekbar;

import android.app.Activity;
import android.graphics.Color;
import android.text.Spannable;
import android.text.SpannableString;
import android.text.style.ForegroundColorSpan;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.SeekBar;
import android.widget.TextView;

import com.hongxun.design.MyApp;
import com.hongxun.design.R;
import com.hongxun.design.mytools.MString;
import com.hongxun.design.mytools.MyDecimalFormat;
import com.hongxun.design.mytools.MyLog;
import com.hongxun.design.utils.DensityUtil;
import com.hongxun.design.utils.maputil.Gps2BaiDu;

/**
 * Created by zerdoor_pc on 2016/5/10.
 */
public class SeekBarUtil {
    private SeekBar seekbar = null;
    private String startTimeStr = "-50";
    private String endTimeStr = "50";
    private TextView text;
    private int totalSeconds = 0;
    private String value;
    TextView result_left, result_int;
    /**
     * 屏幕宽度
     */
    private int screenWidth;
    /**
     * 自定义随着拖动条一起移动的空间
     */
    private TextMoveLayout textMoveLayout;
    /**
     * 托动条的移动步调
     */
    private float moveStep = 0;

    LinearLayout v;
    Activity context;

    public SeekBarUtil(View view, Activity context, TextMoveLayout textMoveLayout,
                       SeekBar seekbar, TextView result_left, TextView result_int) {
        this.seekbar = seekbar;
        this.textMoveLayout = textMoveLayout;
        this.context = context;
        this.result_left = result_left;
        this.result_int = result_int;


        value = result_int.getText().toString();
        if (value.length() > 0) {
            int position = value.length();
//            for (int i=0;i<value.length();i++){
//               if (value.charAt(i) ==".".charAt(0)){
//                   position=position-(i+1)-6;
//               }
//            }


            //创建一个 SpannableString对象
            SpannableString sp = new SpannableString(value);
            sp.setSpan(new ForegroundColorSpan(R.color.project_blue), position - 3, position, Spannable.SPAN_EXCLUSIVE_INCLUSIVE);
            result_int.setText(sp);
        }


        int width = View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED);
        int height = View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED);
        view.measure(width, height);

        screenWidth = seekbar.getMeasuredWidth();
        text = new TextView(context);
        text.setLayoutParams(new ViewGroup.LayoutParams(DensityUtil.dip2px(context, 45), DensityUtil.dip2px(context, 45)));
        text.setBackgroundResource(R.mipmap.bg_wordsfordialog);
        text.setTextColor(Color.rgb(0, 161, 229));
        text.setTextSize(14);
        text.setPadding(0, 10, 0, 0);
        text.setGravity(View.TEXT_ALIGNMENT_GRAVITY);
//        ViewGroup.LayoutParams layoutParams = new ViewGroup.LayoutParams(screenWidth, 50);

        MyLog.L(MyApp.AppName + "progressprogress screenWidth=" + screenWidth + " seekbar" + seekbar.getMeasuredWidth() + "====" + text.getMeasuredHeight());
        this.textMoveLayout.addView(text);

        text.layout(46, 10, DensityUtil.dip2px(context, 45), DensityUtil.dip2px(context, 45));
        text.setVisibility(View.GONE);
        seekbar.setOnSeekBarChangeListener(new OnSeekBarChangeListenerImp());
        searchVideos();
    }

    public void searchVideos() {
        text.setText(startTimeStr);
        totalSeconds = Integer.parseInt(endTimeStr) - Integer.parseInt(startTimeStr);
        seekbar.setEnabled(true);
        seekbar.setMax(totalSeconds);
        seekbar.setProgress(totalSeconds / 2);
        moveStep = (float) (((float) screenWidth / (float) totalSeconds) * 0.9);

    }

    private class OnSeekBarChangeListenerImp implements
            SeekBar.OnSeekBarChangeListener {

        // 触发操作，拖动
        public void onProgressChanged(SeekBar seekBar, int progress,
                                      boolean fromUser) {
            MyLog.L(MyApp.AppName + "progressprogress progress=" + progress + " screenWidth=" + screenWidth);
            MyLog.L(MyApp.AppName + "progressprogress moveStep=" + moveStep);
            MyLog.L(MyApp.AppName + "progressprogress (progress * moveStep=" + (progress * moveStep));
            text.layout((int) (progress * moveStep) + 46, 10, DensityUtil.dip2px(context, 45) + (int) (progress * moveStep), DensityUtil.dip2px(context, 45));

            int values = (progress + Integer.parseInt(startTimeStr));
            text.setText(values + "");
            String nvalue = "" + MyDecimalFormat.getDouble((Double.parseDouble(value) + Math.pow(10, -8) * values));
            MyLog.L(MString.SUCCESSED + "values=" + nvalue);
            MyLog.L(MString.SUCCESSED + "value=" + nvalue);
            result_left.setText(Gps2BaiDu.ConvertDigitalToDegrees(nvalue));
            SpannableString sp = new SpannableString(nvalue);
            int position = nvalue.length();
            sp.setSpan(new ForegroundColorSpan(R.color.project_blue), position - 3, position, Spannable.SPAN_EXCLUSIVE_INCLUSIVE);
            result_int.setText(sp);
        }

        // 表示进度条刚开始拖动，开始拖动时候触发的操作
        public void onStartTrackingTouch(SeekBar seekBar) {
            text.setVisibility(View.VISIBLE);
        }

        // 停止拖动时候
        public void onStopTrackingTouch(SeekBar seekBar) {
            text.setVisibility(View.GONE);
        }
    }
}
